/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Zeshui Xu<i@xuzeshui.com>
 * Created Time: 2015-11-15 19:16:43
 * Last Modified: 2017-07-22 13:09:26
 * File Name: user/userinfo.go
 * Description:
 */
package user

import (
	"time"

	"passport/xfile"
)

/*
	用户信息的修改器
*/
type UserInfoChanger struct {
	userid  string
	newData map[string]interface{}
}

func NewUserInfoChanger(userid string) *UserInfoChanger {
	return &UserInfoChanger{
		userid:  userid,
		newData: make(map[string]interface{}),
	}
}

/*
	将修改器中的信息保存到redis中去
*/
func (p *UserInfoChanger) Save() error {
	if len(p.newData) == 0 { //无更新
		return nil
	}

	p.newData["update_time"] = time.Now().Format("2006-01-02 15:04:05")
	err := GetUserService().setAnyUserInfoByMap(p.userid, p.newData)
	if err == nil {
		item := &xfile.PassportInfoItem{}
		item.TableName = xfile.PI_USER
		if _, ok := p.newData["userid"]; ok {
			item.Op = xfile.OP_INSERT
		} else {
			item.Op = xfile.OP_UPDATE
			p.newData["userid"] = p.userid
		}
		item.Data = p.newData
		xfile.GetPassportInfoMgr().SavePassportInfo(item)
	}
	return err
}

/*
	删除用户信息
*/
func (p *UserInfoChanger) Delete() error {
	err := GetUserService().deleteUserInfo(p.userid)
	if err == nil {
		p.newData["update_time"] = time.Now().Format("2006-01-02 15:04:05")
		p.newData["userid"] = p.userid
		item := &xfile.PassportInfoItem{}
		item.TableName = xfile.PI_USER
		item.Op = xfile.OP_DELETE
		item.Data = p.newData
		xfile.GetPassportInfoMgr().SavePassportInfo(item)
	}
	return nil
}

/*
	设置userid
*/
func (p *UserInfoChanger) SetUserid(userid string) {
	p.newData["userid"] = userid
}

/*
	设置密码
*/
func (p *UserInfoChanger) SetPassword(password string) {
	p.newData["passwd"] = password
}

/*
	设置昵称
*/
func (p *UserInfoChanger) SetNickName(nickName string) {
	if len(nickName) <= 32 {
		p.newData["nick_name"] = nickName
	} else {
		p.newData["nick_name"] = nickName[:32]
	}
}

/*
	设置头像
*/
func (p *UserInfoChanger) SetAvatar(avatar string) {
	p.newData["avatar"] = avatar
}

/*
	设置手机号
*/
func (p *UserInfoChanger) SetMobile(mobile string) {
	p.newData["mobile"] = mobile
}

/*
	设置性别
*/
func (p *UserInfoChanger) SetGender(gender int8) {
	p.newData["gender"] = gender
}

/*
	设置生日
*/
func (p *UserInfoChanger) SetBirthday(birthday string) {
	p.newData["birthday"] = birthday
}

/*
	设置修改签名
*/
func (p *UserInfoChanger) SetSlogan(slogan string) {
	p.newData["slogan"] = slogan
}

/*
	设置身份证号
*/
func (p *UserInfoChanger) SetIdcard(idcard string) {
	p.newData["idcard"] = idcard
}

/*
	设置是否第三方账号
*/
func (p *UserInfoChanger) SetIsThirdAccount(thirdAccount int8) {
	p.newData["third_account"] = thirdAccount
}

/*
	设置自己的邀请码
*/
func (p *UserInfoChanger) SetInviteCode(inviteCode string) {
	p.newData["invite_code"] = inviteCode
}

/*
	设置注册的IP
*/
func (p *UserInfoChanger) SetRegIp(regIp string) {
	p.newData["reg_ip"] = regIp
}

/*
	设置注册平台来源
*/
func (p *UserInfoChanger) SetRegPlat(regPlat int8) {
	p.newData["reg_plat"] = regPlat
}

/*
	设置设备号
*/
func (p *UserInfoChanger) SetVuid(vuid string) {
	p.newData["vuid"] = vuid
}

/*
	注册邀请者来源的邀请码
*/
func (p *UserInfoChanger) SetInviteBy(inviteBy string) {
	p.newData["invite_by"] = inviteBy
}

/*
	设置生成时间
*/
func (p *UserInfoChanger) SetInsertTime(insertTime string) {
	p.newData["insert_time"] = insertTime
}

/*
	设置行政区编码
*/
func (p *UserInfoChanger) SetRegionCode(regionCode string) {
	p.newData["region_code"] = regionCode
}

/*
	设置行政详细地址
*/
func (p *UserInfoChanger) SetRegionAddr(regionAddr string) {
	p.newData["region_addr"] = regionAddr
}

